//
//  HealthViewController.swift
//  groupTasks
//
//  Created by 向银泉 on 16/11/1.
//  Copyright © 2016年 向银泉. All rights reserved.
//

import UIKit
import Alamofire

class HealthViewController: UIViewController {
    var circleView:CirCleView!
    var tableView:UITableView!
    lazy var dataArray:[cirPicModel] = {
        return [cirPicModel]()
    }()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.getData()
        self.creatUI()
        self.CircleUI()

    }
}
extension HealthViewController{
    func CircleUI(){
        self.automaticallyAdjustsScrollViewInsets = false
        let imageArray: [UIImage!] = [UIImage(named: "1.jpg"), UIImage(named: "2.jpg"), UIImage(named: "3.jpg"),UIImage(named:"4.jpg"),UIImage(named:"5.jpg")]
        
        self.circleView = CirCleView(frame: CGRectMake(0, 0, self.view.frame.size.width, 250), imageArray: imageArray)
        circleView.backgroundColor = UIColor.orangeColor()
        self.view.addSubview(circleView)
        self.tableView.tableHeaderView = circleView
        
        self.tableView.rowHeight = 120
        
        
        
    }
    func creatUI(){
        self.tableView = UITableView.init(frame: self.view.bounds, style: .Grouped)
        self.view.addSubview(self.tableView)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.rowHeight = self.view.frame.size.height - 100
        self.tableView.registerNib(UINib.init(nibName:"FoodTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
        
        
        
    }

}
extension HealthViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! FoodTableViewCell
        cell.model = dataArray[indexPath.row] 
        return cell
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let healthDetailC = HealthDetailViewController()
        healthDetailC.ID = self.dataArray[indexPath.row].ID
        healthDetailC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(healthDetailC, animated: true)
    }
    
}
extension HealthViewController{
    func getData(){
        Alamofire.request(.GET, "http://iapi.ipadown.com/api/food/food.show.list.php?keywords=%E7%85%B2%E6%B1%A4&p=1&pagesize=50", parameters: nil, encoding: .URL, headers: nil).responseJSON(options: .MutableContainers) { (data) in
           
                KVNProgress.showSuccessWithStatus("加载成功")
              let json = data.result.value
            if let tarray = json?.objectForKey("results"){
                let array = tarray as! [NSDictionary]
                for dict in array{
                    let model = cirPicModel.init(dict: dict)
                    self.dataArray.append(model)
                }
                self.tableView.reloadData()
                
            }
            
                
            
        }
    }
}

